package com.example.myapplication2;

import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class RadioButtonActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);
        RadioGroup radioGroup = findViewById(R.id.RG);
        radioGroup.setOnCheckedChangeListener(this);
        textView = findViewById(R.id.results);
        //代码选中
        RadioGroup rG2 = findViewById(R.id.RG2);
        rG2.check(R.id.RB2);

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        //其中的i 为选中的值,RadioGroup对象的getCheckedRadioButtonId也可以获取到这个值
        switch (i){
            case R.id.rb_G:
                textView.setText("选中:女");
                break;
            case R.id.rb_M:
                textView.setText("选中:男");
                break;
        }
    }
}